package net.adetech.crj.service;

import net.adetech.crj.dao.UserMapper;
import net.adetech.crj.entity.User;
import net.adetech.crj.service.BaseService;
import net.adetech.crj.utils.InnerServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("user.login")
public class HandleLoginService implements BaseService {
    @Autowired
    UserMapper userMapper;
    @Override
    public Map<String, Object> invoke(Map<String, Object> params) {
        User inputUser = (User)params.get("user");
        User tempUser = userMapper.getUserByIdNUmber(inputUser.getIdNumber());

        if (tempUser!=null)
        {
            if (inputUser.getUserType()==null&&tempUser.getUserType()!=null)
            {
                inputUser.setUserType(tempUser.getUserType());
            }
            userMapper.update(inputUser);
            return InnerServiceUtil.buildResult("user",inputUser);

        }
        else {
            userMapper.insert(inputUser);
            return InnerServiceUtil.buildResult("user",inputUser);
        }

    }
}
